<?php


/*
 * Created on 28.10.2007
 *
 * by MasterGenny
 */

include_once ('BasedMySQLDao.php5');

/**
 *	Data Access Object to interact with the database
 *  controlling value objects of players
 *  extends from BasedMySQLDao 
 */
class ReportDAO extends BasedMySQLDao {

	function ReportDAO() {
		parent :: BasedMySQLDao();
	}

	function findNewsForStartSeite() {
		$db = parent :: db_start();
		$vos = array ();
		$sql = "select r.*, u.usr_email, u.usr_vname, u.usr_name from report r, user u WHERE r.cat<>'TICKER' and r.author_id = u.usr_id order by date desc limit 3;";
		$cursor = mysql_query($sql, $db) or die;
		$anZeilen = mysql_num_rows($cursor);
		if (!empty ($anZeilen)) {
			$i = 0;
			while ($results = mysql_fetch_assoc($cursor)) {
				$vos[$i] = ReportDAO :: map2VO($results);
				$i++;
			}
		} else
			return false;
		parent :: db_stop($db);
		return $vos;
	}

	function findNewsForTeamSeite($teamid) {
		$db = parent :: db_start();
		$vos = array ();
		$sql = "select r.*, u.usr_email, u.usr_vname, u.usr_name from report r, user u " .
				"WHERE r.cat<>'TICKER' and r.author_id = u.usr_id and r.teamid = ".$teamid." " .
						"order by date desc limit 5;";
		$cursor = mysql_query($sql, $db) or die;
		$anZeilen = mysql_num_rows($cursor);
		if (!empty ($anZeilen)) {
			$i = 0;
			while ($results = mysql_fetch_assoc($cursor)) {
				$vos[$i] = ReportDAO :: map2VO($results);
				$i++;
			}
		} else
			return false;
		parent :: db_stop($db);
		return $vos;
	}
	/**
	 * Function is used to map the found db-entry on a value object
	 * @param ResultSet $result - result of a select
	 */
	function map2VO($result) {
		$vo = new ReportVO();
		$vo->id = $result["report_id"];
		$vo->cat = $result["cat"];
		$vo->team = $result["team"];
		$vo->headline = $result["headline"];
		$vo->text = $result["text"];
		$vo->date = $result["date"];
		$vo->authorId = $result["author_id"];
		$vo->authorMail = $result["usr_email"];
		$vo->authorName = $result["usr_vname"] . " " . $result["usr_name"];
		if (!empty($result["img"]))
			$vo->img = $result["img"];
		if (!empty($result["img_th"]))
			$vo->imgTh = $result["img_th"];
		if (!empty($result["img_subtitle"]))
			$vo->imgSubTitle = $result["img_subtitle"];		
		return $vo;
	}
}
?>
